<?php

namespace app\model;

use think\Model;

class Post extends Model
{
    public static function getPosts($query)
    {
        return self::where('status', 1)
            ->where('isdelete', 0)
            ->order('update_time', 'DESC')
            ->paginate(5, false, [
                'query' => $query,
                'type' => 'custom'
            ]);
    }

    public static function getPostsByPage($page)
    {
        $offset = 20 * intval($page);
        return self::where('status', 1)
            ->where('isdelete', 0)
            ->order('update_time', 'DESC')
            ->limit($offset, 20)
            ->select();
    }

    public static function getItems($limit = 3)
    {
        return self::where('status', 1)
            ->where('isdelete', 0)
            ->order('update_time', 'DESC')
            ->limit($limit)
            ->select();
    }

    public function getCreateTimeAttr($v)
    {
        return intval($v);
    }

    public function getPosterAttr($v)
    {
        return config('site.imgUrl') . $v;
    }

    public function category()
    {
        return $this->belongsTo('PostMenu', 'category_id', 'id');
    }
}
